<script setup lang="ts" name="OnlineEdit">
import { addOnlineApi, getOnlineApi, updateOnlineApi } from "@/api/monitor/online";

const { proxy } = getCurrentInstance();

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    default: null,
  },
  title: {
    type: String,
    default: false,
  }
});

// 在线用户表单
const onlineFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<OnlineRecord>({});

// 表单校验
const rules = ref({
});

// 加载
const loading = ref(false);

/** 获取在线用户详情 */
function getData() {
  reset();
  if (isEmpty(props.id)) {
    open.value = true;
  } else {
    getOnlineApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(err => {
      console.error(err);
      closeDialog(false);
    })
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    tokenId: null,
    userName: null,
    deptName: null,
    ipaddr: null,
    loginLocation: null,
    os: null,
    browser: null,
    loginTime: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  onlineFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (isEmpty(form.value.onlineId)) {
        addOnlineApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(err => {
          console.error(err);
          loading.value = false;
        });
      } else {
        updateOnlineApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(err => {
          console.error(err);
          loading.value = false;
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
});

getData();
</script>
<template>
  <!-- 在线用户编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="onlineFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="会话编号" prop="tokenId">
        <el-input v-model="form.tokenId" placeholder="请输入会话编号" />
      </el-form-item>
      <el-form-item label="登录名称" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入登录名称" />
      </el-form-item>
      <el-form-item label="所属部门" prop="deptName">
        <el-input v-model="form.deptName" placeholder="请输入所属部门" />
      </el-form-item>
      <el-form-item label="主机" prop="ipaddr">
        <el-input v-model="form.ipaddr" placeholder="请输入主机" />
      </el-form-item>
      <el-form-item label="登录地点" prop="loginLocation">
        <el-input v-model="form.loginLocation" placeholder="请输入登录地点" />
      </el-form-item>
      <el-form-item label="操作系统" prop="os">
        <el-input v-model="form.os" placeholder="请输入操作系统" />
      </el-form-item>
      <el-form-item label="浏览器" prop="browser">
        <el-input v-model="form.browser" placeholder="请输入浏览器" />
      </el-form-item>
      <el-form-item label="登录时间" prop="loginTime">
        <el-date-picker
            v-model="form.loginTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择登录时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>

</style>